package apps.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.GrantedAuthority;
import org.springframework.web.servlet.ModelAndView;

import webframe.common.helper.ControllerHelper;
import apps.web.controller.base.MyBaseController;
import apps.web.model.User;

public class MultiViewController extends MyBaseController {
	// accessdenied
	public ModelAndView accessDenied(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("AccessDenied.jsp");
	}

	// home
	public ModelAndView home(HttpServletRequest request,
			HttpServletResponse response) {
		Map home = new HashMap();
		home.put("accessTime", new Date());
		home.put("ip", request.getRemoteAddr());
		return new ModelAndView("Home.jsp", "home", home);
	}

	// menu
	public ModelAndView menu(HttpServletRequest request,
			HttpServletResponse response) {
		boolean admin = false;
		User user = (User) ControllerHelper.getUserBySession();
		Map map = new HashMap();
		GrantedAuthority[] authorities = user.getAuthorities();
		for (GrantedAuthority authority : authorities) {
			if (authority.getAuthority().equalsIgnoreCase("ROLE_ADMIN")) {
				admin = true;
			}
		}
		String[] rights = new String[authorities.length];
		int i = 0;
		for (GrantedAuthority grantedAuthority : authorities) {
			String s = grantedAuthority.getAuthority();
			rights[i] = s;
			i++;
		}

		map.put("user", user);
		map.put("rights", rights);
		map.put("admin", admin);
		return new ModelAndView("Menu.jsp", "map", map);
	}

	// //////////////////////////////////////////////////////////
	public ModelAndView defaultView(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("");
	}
}
